home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / for.test < prev    next >
Encoding:
Text File  |  1994-12-18  |  4.0 KB  |  156 lines

  1. # Commands covered:  foreach, for, continue, break
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) for.test 1.9 94/12/17 16:19:57
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. # Basic "foreach" operation.
  18.  
  19. test for-1.1 {basic foreach tests} {
  20.     set a {}
  21.     foreach i {a b c d} {
  22.     set a [concat $a $i]
  23.     }
  24.     set a
  25. } {a b c d}
  26. test for-1.2 {basic foreach tests} {
  27.     set a {}
  28.     foreach i {a b {{c d} e} {123 {{x}}}} {
  29.     set a [concat $a $i]
  30.     }
  31.     set a
  32. } {a b {c d} e 123 {{x}}}
  33. test for-1.3 {basic foreach tests} {catch {foreach} msg} 1
  34. test for-1.4 {basic foreach tests} {
  35.     catch {foreach} msg
  36.     set msg
  37. } {wrong # args: should be "foreach varName list command"}
  38. test for-1.5 {basic foreach tests} {catch {foreach i} msg} 1
  39. test for-1.6 {basic foreach tests} {
  40.     catch {foreach i} msg
  41.     set msg
  42. } {wrong # args: should be "foreach varName list command"}
  43. test for-1.7 {basic foreach tests} {catch {foreach i j} msg} 1
  44. test for-1.8 {basic foreach tests} {
  45.     catch {foreach i j} msg
  46.     set msg
  47. } {wrong # args: should be "foreach varName list command"}
  48. test for-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1
  49. test for-1.10 {basic foreach tests} {
  50.     catch {foreach i j k l} msg
  51.     set msg
  52. } {wrong # args: should be "foreach varName list command"}
  53. test for-1.11 {basic foreach tests} {
  54.     set a {}
  55.     foreach i {} {
  56.     set a [concat $a $i]
  57.     }
  58.     set a
  59. } {}
  60. test for-1.11 {foreach errors} {
  61.     catch {unset a}
  62.     set a(0) 44
  63.     list [catch {foreach a {1 2 3} {}} msg] $msg
  64. } {1 {couldn't set loop variable}}
  65. catch {unset a}
  66.  
  67. # Check "continue".
  68.  
  69. test for-2.1 {continue tests} {catch continue} 4
  70. test for-2.2 {continue tests} {
  71.     set a {}
  72.     foreach i {a b c d} {
  73.     if {[string compare $i "b"] == 0} continue
  74.     set a [concat $a $i]
  75.     }
  76.     set a
  77. } {a c d}
  78. test for-2.3 {continue tests} {
  79.     set a {}
  80.     foreach i {a b c d} {
  81.     if {[string compare $i "b"] != 0} continue
  82.     set a [concat $a $i]
  83.     }
  84.     set a
  85. } {b}
  86. test for-2.4 {continue tests} {catch {continue foo} msg} 1
  87. test for-2.5 {continue tests} {
  88.     catch {continue foo} msg
  89.     set msg
  90. } {wrong # args: should be "continue"}
  91.  
  92. # Check "break".
  93.  
  94. test for-3.1 {break tests} {catch break} 3
  95. test for-3.2 {break tests} {
  96.     set a {}
  97.     foreach i {a b c d} {
  98.     if {[string compare $i "c"] == 0} break
  99.     set a [concat $a $i]
  100.     }
  101.     set a
  102. } {a b}
  103. test for-3.3 {break tests} {catch {break foo} msg} 1
  104. test for-3.4 {break tests} {
  105.     catch {break foo} msg
  106.     set msg
  107. } {wrong # args: should be "break"}
  108.  
  109. # Check "for" and its use of continue and break.
  110.  
  111. test for-4.1 {for tests} {
  112.     set a {}
  113.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  114.     set a [concat $a $i]
  115.     }
  116.     set a
  117. } {1 2 3 4 5}
  118. test for-4.2 {for tests} {
  119.     set a {}
  120.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  121.     if $i==4 continue
  122.     set a [concat $a $i]
  123.     }
  124.     set a
  125. } {1 2 3 5}
  126. test for-4.3 {for tests} {
  127.     set a {}
  128.     for {set i 1} {$i<6} {set i [expr $i+1]} {
  129.     if $i==4 break
  130.     set a [concat $a $i]
  131.     }
  132.     set a
  133. } {1 2 3}
  134. test for-4.4 {for tests} {catch {for 1 2 3} msg} 1
  135. test for-4.5 {for tests} {
  136.     catch {for 1 2 3} msg
  137.     set msg
  138. } {wrong # args: should be "for start test next command"}
  139. test for-4.6 {for tests} {catch {for 1 2 3 4 5} msg} 1
  140. test for-4.7 {for tests} {
  141.     catch {for 1 2 3 4 5} msg
  142.     set msg
  143. } {wrong # args: should be "for start test next command"}
  144. test for-4.8 {for tests} {
  145.     set a {xyz}
  146.     for {set i 1} {$i<6} {set i [expr $i+1]} {}
  147.     set a
  148. } xyz
  149. test for-4.9 {for tests} {
  150.     set a {}
  151.     for {set i 1} {$i<6} {set i [expr $i+1]; if $i==4 break} {
  152.     set a [concat $a $i]
  153.     }
  154.     set a
  155. } {1 2 3}
  156.